/*----------------------------------------------
| noka input v1.0 www.97521.com                 |
| rebin 2014-08-14                             |
|---------------------------------------------*/
var ntextarea = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.id=cfg.id; //id
    	this.cfg = cfg;
    	this.rurl=cfg.rurl;//根路径
    	this.showinfo = cfg.showinfo;
    	this.charsize = cfg.charsize | 200;//字数
    	this.verifun = cfg.verifun;//提交时调用的自定义方法
    	this.lang_1=cfg.lang_1;
    	this.lang_2=cfg.lang_2;
    	this.lang_3=cfg.lang_3;
    	this.lang_4=cfg.lang_4;
    	this.lang_5=cfg.lang_5;
    	this.lang_mesg=cfg.lang_mesg;
    },
    show : function(){
    	this.textarCreat();
    	this.TextareaEvent();
    	this.UpdateInfo();//初始化数据
    },
    //-----------------创建-----------------------------
    textarCreat : function(){
    	var idx = 0;
        var html = [];
        var self = this;
        var bodyHtml=$(self.id+'_infoDiv');
        if(undefined!=self.showinfo && !self.showinfo){
        	bodyHtml.hide();
        }
        html[idx++] = self.lang_1;//
        html[idx++] = '<input disabled  name="total" id="'+self.id+'_intotal"  value="'+self.charsize+'" style="border:none; background:#fff;color:#c1c1c1; width:30px;">';
        html[idx++] = self.lang_2;//已用字数：
        html[idx++] = '<input disabled  name="used" id="'+self.id+'_inused" value="0"  style="border:none; background:#fff;color:#c1c1c1; width:30px;">';
        html[idx++] = self.lang_3;//剩余字数：
        html[idx++] = '<input disabled  name="remain" id="'+self.id+'_inremain"  value="'+self.charsize+'"  style="border:none; background:#fff;color:#c1c1c1; width:30px;">';
        bodyHtml.innerHTML=html.join('');
    },
    TextareaEvent : function(){
    	var self = this;
    	var obj = $(self.id);
    	obj.observe('keydown',function(a){
    		self.UpdateInfo();
    	});
    	obj.observe('keyup',function(a){
    		self.UpdateInfo();
    	});
    	obj.observe('blur',function(){
    		self.UpdateInfo();
     	});
    	obj.observe('mousemove',function(){
     		self.setSelectStyle(obj);
     	});
        obj.observe('mouseout',function(event){
         	var element = event.element();
         	setTimeout(function(){
                 self.setNoSelectStyle(obj);
             }, 500);
         });
        $(self.id).veri = function(){
        	if(undefined!=self.verifun){
        		try{return self.verifun($(self.id));}catch(e){}
        	}
        	self.UpdateInfo();
        	return true;
        };
        $(self.id).setValue = function(value,ch){
    		$(self.id).value = value;//设置值
    	};
    	 $(self.id).setDisabled = function(a){
      		$(self.id).writeAttribute({disabled:a});
      	};
      	$(self.id).setReadonly = function(a){
      		$(self.id).writeAttribute({readonly:a});
      	};
    	document.observe('widget:noka_reinit',function(event){//失失焦点时验证其值
        	if(self.id.indexOf('_nkADinsert_')==-1){
     	    	var npfffs=event.memo.widgetNumber.clone();
     	    	for(var i=0;i<npfffs.length;i++){
     	    		var cfgid = Object.allclone(self.cfg);
     	    		var initem = npfffs[i];
	    			cfgid.id = initem.id;
	    			if(undefined!=initem.value){
	    				cfgid.value=initem.value;
	    			}
     	    		if(cfgid.id.indexOf('_'+self.id+'_')!=-1){
     	    			(new ntextarea(cfgid)).show();
     	    			try{
	    					var idx = cfgid.id.substring(cfgid.id.lastIndexOf('_')+1,cfgid.id.length);
	    					$(cfgid.id).name=$(cfgid.id).name.replace(/\[0\]/g,'['+idx+']');
	    					if(undefined!=initem.value && $(cfgid.id).name==initem.name){
	    						$(cfgid.id).setValue(initem.value);
	    					}
	    				}catch(e){}
     	    		}
     	    	}
        	}
    	});
    },
    UpdateInfo : function(){
	 var max;
	 var self = this;
	 var total = $(self.id+'_intotal');
	 var used = $(self.id+'_inused');
	 var remain = $(self.id+'_inremain');
	 var myid = $(self.id);
	 var msg = self.lang_4+self.charsize+self.lang_5;
	 if(undefined !=self.lang_mesg){
		 msg = self.lang_mesg;
	 }
	 max = total.value;
	 if (myid.value.length > max) {
		 myid.value = myid.value.substring(0,max);
		 used.value = max;
		 remain.value = 0;
		 alert(msg);
	 }else {
		 used.value = myid.value.length;
		 remain.value = max - used.value;
	 }
    },
   //----------选种状态-----------------------
    setSelectStyle : function(obj){
    	obj.setStyle({borderColor:'#5794bf'});
    },
    //-----------未选种状态-------------------
    setNoSelectStyle : function(obj){
    	obj.setStyle({borderColor:'#AFAFAF'});
    }
});